﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace DnDEngine.WebEngine
{
    public static class HttpRequestBuilder
    {
        public static HttpWebRequest HttpRequestFactory(String sUrl, String sMethod)
        {
            try
            {
                Logger.Log.TraceAction("HttpRequestBuilder.HttpRequestFactory", sUrl + ";" + sMethod);
                var myReq = (HttpWebRequest)WebRequest.Create(sUrl);
                myReq.Method = sMethod;
                myReq.KeepAlive = false;
                myReq.Timeout = 60000;
                return myReq;
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("HttpRequestBuilder.HttpRequestFactory", ex);
                return null;
            }

        }

    }

    public static class HtmlParser
    {

        public static List<String> GetTableLines(String sData)
        {
            var tableLine = new List<string>();
            Logger.Log.TraceAction("HtmlParser.GetTableLines", sData);
            try
            {

                sData = sData.Substring(sData.IndexOf("<table"), (sData.IndexOf("</table") - sData.IndexOf("<table") + 6));

                var table = sData.Split(new string[] { "<", ">" }, StringSplitOptions.RemoveEmptyEntries);

                for (int i = 0; i < table.Length; i++)
                {
                    if (table[i].Contains("td") && table[i] != "/td")
                    {
                        tableLine.Add(table[i + 1]);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("HtmlParser.GetTableLines", ex);
            }
            return tableLine;
        }
        /// <summary>
        ///  Parse HTMl data using delimiting parameter
        /// </summary>
        /// <param name="sData">HTML to parse</param>
        /// <param name="oCell">Opening cell</param>
        /// <param name="cCell">Closing cell</param>
        /// <returns></returns>

        public static List<String[]> GetContent(String sData, String oCell, String cCell)
        {

            List<String[]> eData = new List<String[]>();
            try
            {
                Logger.Log.TraceAction("HtmlParser.GetContent", "");

                if (sData.Contains(oCell) && sData.Contains(cCell))
                {
                    while (sData.Contains(oCell))
                    {
                        //Get String which contain <cell> and </cell>
                        var TempData = sData.Substring(sData.IndexOf(oCell), sData.IndexOf(cCell) - sData.IndexOf(oCell));
                        //Split temp Data to obtain a String table: easy to work with
                        eData.Add(TempData.Split(new Char[] { '<', '>' }));
                        //Remove current cell from Data
                        sData = sData.Remove(sData.IndexOf(oCell), sData.IndexOf(cCell) - sData.IndexOf(oCell));
                    }
                }
                else
                {
                    Logger.Log.TraceError("HtmlParser.GetContent", new Exception("cCell not found"));
                }

            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("HttpRequestBuilder.HtmlParser.GetContent", ex);

            }
            finally
            {
            }
            return eData;
        }

        /// <summary>
        /// Return value from cell like value=""
        /// </summary>
        /// <param name="sData">HTML data</param>
        /// <returns></returns>
        public static Dictionary<String, String> GetValue(String sData)
        {
            var eData = new Dictionary<String, String>();

            try
            {
                Logger.Log.TraceAction("HtmlParser.GetValue", sData);
                var tData = sData.Split(char.Parse("\""));

                for (int i = 0; i < sData.Length; i++)
                {
                    if (tData[i].Contains("="))
                    {
                        eData.Add(tData[i].Replace("=", ""), tData[i + 1]);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError(System.Reflection.MethodBase.GetCurrentMethod().ToString(), ex);
            }
            return eData;
        }

        private static int GetCharPos(String sData, char cSearch)
        {
            var pos = 0;
            for (int i = 0; i < sData.Length; i++)
            {
                if (sData[i] == cSearch)
                {
                    pos = i;
                    break;
                }
            }
            return pos;
        }

        public static List<String> GetOptionValue(String sData)
        {
            var optionVal = new List<string>();
            try
            {
                Logger.Log.TraceAction("HtmlParser.GetOptionValue", sData);
                sData = sData.Substring(sData.IndexOf("<option"), (sData.IndexOf("</option") - sData.IndexOf("<option")));

                var dataArray = GetValue(sData);

                foreach (var st in dataArray)
                {
                    optionVal.Add(st.Value);
                }
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("HtmlParser.GetOptionValue", ex);
            }
            return optionVal;
        }

    }
}
